package com.openbravo.pos.scanpal2;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class DeviceScannerComm
  implements DeviceScanner, SerialPortEventListener
{
  private CommPortIdentifier m_PortIdPrinter;
  private SerialPort m_CommPortPrinter;
  private String m_sPort;
  private OutputStream m_out;
  private InputStream m_in;
  private static final byte[] COMMAND_READ = { 82, 69, 65, 68 };
  private static final byte[] COMMAND_CIPHER = { 67, 73, 80, 72, 69, 82 };
  private static final byte[] COMMAND_OVER = { 79, 86, 69, 82 };
  private static final byte[] COMMAND_ACK = { 65, 67, 75 };
  private Queue<byte[]> m_aLines;
  private ByteArrayOutputStream m_abuffer;
  private int m_iStatus;
  private int m_iProductOrder;

  DeviceScannerComm(String paramString)
  {
    this.m_sPort = paramString;
    this.m_PortIdPrinter = null;
    this.m_CommPortPrinter = null;
    this.m_out = null;
    this.m_in = null;
  }

  public void connectDevice()
    throws DeviceScannerException
  {
    try
    {
      this.m_PortIdPrinter = CommPortIdentifier.getPortIdentifier(this.m_sPort);
      this.m_CommPortPrinter = ((SerialPort)this.m_PortIdPrinter.open("PORTID", 2000));
      this.m_out = this.m_CommPortPrinter.getOutputStream();
      this.m_in = this.m_CommPortPrinter.getInputStream();
      this.m_CommPortPrinter.addEventListener(this);
      this.m_CommPortPrinter.notifyOnDataAvailable(true);
      this.m_CommPortPrinter.setSerialPortParams(115200, 8, 1, 0);
    }
    catch (Exception localException)
    {
      this.m_PortIdPrinter = null;
      this.m_CommPortPrinter = null;
      this.m_out = null;
      this.m_in = null;
      throw new DeviceScannerException(localException);
    }
    synchronized (this)
    {
      this.m_aLines = new LinkedList();
      this.m_abuffer = new ByteArrayOutputStream();
    }
  }

  public void disconnectDevice()
  {
    try
    {
      this.m_out.close();
      this.m_in.close();
      this.m_CommPortPrinter.close();
    }
    catch (IOException localIOException)
    {
    }
    synchronized (this)
    {
      this.m_aLines = null;
      this.m_abuffer = null;
    }
    this.m_PortIdPrinter = null;
    this.m_CommPortPrinter = null;
    this.m_out = null;
    this.m_in = null;
  }

  public void startDownloadProduct()
    throws DeviceScannerException
  {
    writeLine(COMMAND_READ);
    readCommand(COMMAND_ACK);
  }

  public ProductDownloaded recieveProduct()
    throws DeviceScannerException
  {
    byte[] arrayOfByte = readLine();
    if (checkCommand(COMMAND_OVER, arrayOfByte))
      return null;
    ProductDownloaded localProductDownloaded = new ProductDownloaded();
    try
    {
      String str1 = new String(arrayOfByte, 1, arrayOfByte.length - 3, "ISO-8859-1");
      StringTokenizer localStringTokenizer = new StringTokenizer(str1, "|");
      while (localStringTokenizer.hasMoreTokens())
      {
        String str2 = localStringTokenizer.nextToken();
        if (str2.startsWith("IEAN"))
          localProductDownloaded.setCode(str2.substring(4).trim());
        else if (str2.startsWith("ICANT"))
          try
          {
            localProductDownloaded.setQuantity(Double.parseDouble(str2.substring(5).trim()));
          }
          catch (NumberFormatException localNumberFormatException)
          {
          }
      }
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    writeLine(COMMAND_ACK);
    return localProductDownloaded;
  }

  public void startUploadProduct()
    throws DeviceScannerException
  {
    writeLine(COMMAND_CIPHER);
    readCommand(COMMAND_ACK);
    this.m_iProductOrder = 0;
  }

  public void sendProduct(String paramString1, String paramString2, Double paramDouble)
    throws DeviceScannerException
  {
    this.m_iProductOrder += 1;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    try
    {
      localByteArrayOutputStream.write(convert(Integer.toString(this.m_iProductOrder)));
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(convert(paramString1));
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(convert(paramString2));
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(convert(paramDouble.toString()));
      localByteArrayOutputStream.write(124);
      localByteArrayOutputStream.write(calcCheckSum1(localByteArrayOutputStream.toByteArray()));
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
    }
    catch (IOException localIOException)
    {
    }
    writeLine(localByteArrayOutputStream.toByteArray());
    readCommand(COMMAND_ACK);
  }

  public void stopUploadProduct()
    throws DeviceScannerException
  {
    writeLine(COMMAND_OVER);
    readCommand(COMMAND_ACK);
  }

  private void readCommand(byte[] paramArrayOfByte)
    throws DeviceScannerException
  {
    byte[] arrayOfByte = readLine();
    if (!checkCommand(paramArrayOfByte, arrayOfByte))
      throw new DeviceScannerException("Command not expected");
  }

  private void writeLine(byte[] paramArrayOfByte)
    throws DeviceScannerException
  {
    if (this.m_CommPortPrinter == null)
      throw new DeviceScannerException("No Serial port opened");
    synchronized (this)
    {
      try
      {
        this.m_out.write(paramArrayOfByte);
        this.m_out.write(13);
        this.m_out.flush();
      }
      catch (IOException localIOException)
      {
        throw new DeviceScannerException(localIOException);
      }
    }
  }

  private byte[] readLine()
    throws DeviceScannerException
  {
    synchronized (this)
    {
      if (!this.m_aLines.isEmpty())
        return (byte[])this.m_aLines.poll();
      try
      {
        wait(1000L);
      }
      catch (InterruptedException localInterruptedException)
      {
      }
      if (this.m_aLines.isEmpty())
        throw new DeviceScannerException("Timeout");
      return (byte[])this.m_aLines.poll();
    }
  }

  private byte[] convert(String paramString)
  {
    if (paramString == null)
      return new byte[0];
    byte[] arrayOfByte = new byte[paramString.length()];
    for (int i = 0; i < paramString.length(); i++)
    {
      int j = paramString.charAt(i);
      if (j == 124)
        arrayOfByte[i] = 46;
      else if ((j >= 32) && (j < 128))
        arrayOfByte[i] = (byte)j;
      else
        arrayOfByte[i] = 32;
    }
    return arrayOfByte;
  }

  private byte[] calcCheckSum1(byte[] paramArrayOfByte)
  {
	  //ati fix
    int i = 0;
    for (int j = 0; j < paramArrayOfByte.length; j++)
      i += paramArrayOfByte[j];
    byte j = (byte)((i & 0xFF00) >> 8);
    if (j == 13)
      j = 14;
    int k = (byte)(i & 0xFF);
    if (k == 13)
      k = 14;
    byte[] arrayOfByte = new byte[2];
    arrayOfByte[0] = j;
    arrayOfByte[1] = (byte) k;
    return arrayOfByte;
  }

  private boolean checkCommand(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2)
  {
    if (paramArrayOfByte1.length == paramArrayOfByte2.length)
    {
      for (int i = 0; i < paramArrayOfByte1.length; i++)
        if (paramArrayOfByte1[i] != paramArrayOfByte2[i])
          return false;
      return true;
    }
    return false;
  }

  public void serialEvent(SerialPortEvent paramSerialPortEvent)
  {
    switch (paramSerialPortEvent.getEventType())
    {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
      break;
    case 1:
      try
      {
        while (this.m_in.available() > 0)
        {
          int i = this.m_in.read();
          synchronized (this)
          {
            if (i == 13)
            {
              this.m_aLines.add(this.m_abuffer.toByteArray());
              this.m_abuffer.reset();
              notifyAll();
            }
            else
            {
              this.m_abuffer.write(i);
            }
          }
        }
      }
      catch (IOException localIOException)
      {
      }
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.scanpal2.DeviceScannerComm
 * JD-Core Version:    0.6.0
 */